using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WCSFContrib.ECommerceCatalog.BusinessEntities;
using WCSFContrib.ECommerceCatalog.BusinessEntities.Enums;
using WCSFContrib.ECommerceCatalog.CatalogData;
using WCSFContrib.ECommerceCatalog.User.Services;
using WCSFContrib.ECommerceCatalog.User.Tests.Fakes;

namespace WCSFContrib.ECommerceCatalog.User.Tests.Services
{
    /// <summary>
    /// Summary description for OrdersServiceFixture
    /// </summary>
    [TestClass]
    public class OrdersServiceFixture
    {
        private CatalogDataSet ds;
        private OrdersService ordersService;

        [TestInitialize()]
        public void MyTestInitialize()
        {
            ds = new CatalogDataSet();

            ds.Category.AddCategoryRow("C1", "General1", 0);
            ds.Category.AddCategoryRow("C2", "General2", 0);
            ds.Category.AddCategoryRow("C1-A", "1-SubGeneral1", 1);
            ds.Category.AddCategoryRow("C1-B", "1-SubGeneral2", 1);
            ds.Category.AddCategoryRow("C2-A", "General3", 2);

            ds.Item.AddItemRow("Item1Description", "item1.png", "Item1", 11, 21, ds.Category[0]);
            ds.Item.AddItemRow("Item2Description", "item2.png", "Item2", 12, 22, ds.Category[1]);
            ds.Item.AddItemRow("Item3Description", "item3.png", "Item3", 13, 23, ds.Category[2]);
            ds.Item.AddItemRow("Item4Description", "item4.png", "Item4", 14, 24, ds.Category[0]);
            ds.Item.AddItemRow("Item5Description", "item5.png", "Item5", 15, 25, ds.Category[1]);

            ds.Address.AddAddressRow("One Microsoft Way", "Redmond", "Seattle", "United States", "5555-5555");

            ds.OrderStatus.AddOrderStatusRow(1, "Open");
            ds.OrderStatus.AddOrderStatusRow(2, "ReadyToShip");
            ds.OrderStatus.AddOrderStatusRow(3, "Shipped");
            ds.OrderStatus.AddOrderStatusRow(4, "Canceled");

            ds.PaymentType.AddPaymentTypeRow(1, "CreditCard");
            ds.PaymentType.AddPaymentTypeRow(2, "BankAccount");
            ds.PaymentType.AddPaymentTypeRow(3, "Checks");

            ds.Payment.AddPaymentRow(3, "No info");

            ds.Order.AddOrderRow("jdoe", DateTime.Now, 1, 1, 2, 1);
            ds.Order.AddOrderRow("jdoe", DateTime.Now, 1, 1, 2, 1);

            ds.OrderItem.AddOrderItemRow(ds.Order[0], ds.Item[2], 2);
            ds.OrderItem.AddOrderItemRow(ds.Order[0], ds.Item[0], 1);
            ds.OrderItem.AddOrderItemRow(ds.Order[1], ds.Item[4], 2);

            ds.AcceptChanges();

            ordersService = new OrdersService(ds, new FakeItemsService());
        }

        [TestMethod]
        public void ShouldGetOrderById()
        {
            Order order = ordersService.GetOrderById(1001);
            Assert.AreEqual<int>(1001, order.OrderId);
            Assert.AreEqual<OrderStatus>(OrderStatus.ReadyToShip, order.OrderStatus);
        }

        [TestMethod]
        public void ShouldGetOrdersByUserName()
        {
            ICollection<Order> orders = ordersService.GetOrdersByUserName("jdoe");
            Assert.AreEqual<int>(2, orders.Count);
        }

        [TestMethod]
        public void ShouldGetAllOrders()
        {
            ICollection<Order> allOrders = ordersService.AllOrders;
            Assert.IsNotNull(allOrders);
            Assert.AreEqual<int>(2, allOrders.Count);
        }

        [TestMethod]
        public void ShouldGetOrdersByOrderStatus()
        {
            ICollection<Order> readyToShipOrders = ordersService.GetOrdersByOrderStatus(OrderStatus.ReadyToShip);

            Assert.IsNotNull(readyToShipOrders);
            Assert.AreEqual<int>(2, readyToShipOrders.Count);
        }

        [TestMethod]
        public void ShouldChangeOrderStatus()
        {
            ordersService.ChangeOrderStatus(1001, OrderStatus.Shipped);
            Order order = ordersService.GetOrderById(1001);
            ICollection<Order> readyToShipOrders = ordersService.GetOrdersByOrderStatus(OrderStatus.ReadyToShip);

            Assert.IsNotNull(order);
            Assert.AreEqual<int>(1, readyToShipOrders.Count);
            Assert.AreEqual<OrderStatus>(order.OrderStatus, BusinessEntities.Enums.OrderStatus.Shipped);
        }
    }
}
